package com.mapforge.splash;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.HeadlessException;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import com.mapforge.Constants;
import com.mapforge.listener.LoadChangeEvent;

public class MapEditorSplashScreen extends JFrame implements ChangeListener {

	/**
	 * Default serial version ID, here to remove warnings.
	 */
	private static final long serialVersionUID = 1L;

	private static final Dimension SIZE = new Dimension(455, 295);
	private static final ImageIcon SPLASH_ICON = Constants.SPLASHSCREEN_ICON;

	private MapEditorLoader mapEditorLoader;

	private JLabel statusLabel;
	private JProgressBar progressBar;
	private CloseWindowLabel closeLabel;

	public MapEditorSplashScreen() throws HeadlessException {
		super();
		mapEditorLoader = new MapEditorLoader(this);
		setUndecorated(true);
		setSize(SIZE);
		setLocationRelativeTo(null);
		progressBar = new JProgressBar();
		closeLabel = new CloseWindowLabel();
		JPanel glassPanel = new JPanel() {
			private static final long serialVersionUID = 1L;

			public void paintComponent(Graphics g) {
				super.paintComponent(g);
				SPLASH_ICON.paintIcon(this, g, 0, 0);
			}
		};
		statusLabel = new JLabel("Progress...");
		glassPanel.add(closeLabel);
		glassPanel.add(statusLabel);
		glassPanel.setLayout(null);
		statusLabel.setBounds(2, SIZE.height - 40, SIZE.width - 2, 20);
		closeLabel.setLocation(SIZE.width - closeLabel.getWidth() - 5, 5);
		glassPanel.add(closeLabel);
		progressBar.setBounds(0, SIZE.height - 20, SIZE.width, 20);
		glassPanel.add(progressBar);
		add(glassPanel);
		try {
			new Thread(mapEditorLoader).start();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		setVisible(true);
	}

	public void setVisible(boolean b) {
		this.getGlassPane().setVisible(true);
		super.setVisible(b);
	}

	public static void main(String[] args) {
		new MapEditorSplashScreen();
	}

	private class CloseWindowLabel extends JLabel implements MouseListener {
		private static final long serialVersionUID = 1L;
		private ImageIcon focusedIcon;
		private ImageIcon unfocusedIcon;

		public CloseWindowLabel() {
			focusedIcon = Constants.WINDOW_FOCUSED_ICON;
			unfocusedIcon = Constants.WINDOW_UNFOCUSED_ICON;
			setIcon(unfocusedIcon);
			this.addMouseListener(this);
			setSize(20, 20);
			this.setOpaque(false);
		}

		public void mouseClicked(MouseEvent arg0) {

		}

		public void mouseEntered(MouseEvent arg0) {
			this.setIcon(focusedIcon);
		}

		public void mouseExited(MouseEvent arg0) {
			setIcon(unfocusedIcon);
		}

		public void mousePressed(MouseEvent arg0) {

		}

		public void mouseReleased(MouseEvent arg0) {

		}
	}

	public void stateChanged(ChangeEvent e) {
		if (e instanceof LoadChangeEvent) {
			LoadChangeEvent event = (LoadChangeEvent) e;
			statusLabel.setText(event.details);
			if (event.progress == 100) {
				try {
					setVisible(false);
					dispose();
				} catch (Exception e2) {
					e2.printStackTrace();
				}
			} else
				this.progressBar.setValue(event.progress);
		}
	}
}